package com.search.examples;


import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * @Author: Yinliu Hao
 * @Description:
 * @Date: Created in 7/11/2023 4:50 PM
 * @Modify By:
 */
public class HttpTool {

    private static String cookie = "SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWkD4QwU6wGr7JF2qm8SWdI5JpX5KMhUgL.FozE1KBX1hqXSKe2dJLoIEXLxKMLBKMLBKqLxK-LBozLB-BLxKML1-2L1hBLxKMLBK.LB.2LxK.L1KMLBoBt; SCF=AvzzexvkxlMH0zuO46MbgzFupK-BM3ZKbfauufv21HErKui89qCgxzAmrvf6ohMWYDsoShya6Vfwfm07pNG4RVE.; SUB=_2A25LikdaDeRhGeRM4lYV-CjIzj-IHXVo5saSrDV6PUJbktANLXHVkW1NU6p3sJPweA26LNRcX7FiVCtjKg1ivMIu; SSOLoginState=1720596234; ALF=1723188234; _T_WM=56917285498; WEIBOCN_FROM=1110006030; XSRF-TOKEN=74131e; MLOGIN=1; M_WEIBOCN_PARAMS=luicode%3D10000011%26lfid%3D231583%26uicode%3D10000011%26fid%3D1076031444865141";
    static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36";
    static int getCount = 0;

    public static JSONObject get(String url,int reTry) throws Exception {

        JSONObject root = null;
        String ret = null;
        getCount++;
        try {
            /*HttpHost proxy = new HttpHost("127.0.0.1", 10809, "http");

            //把代理设置到请求配置
            RequestConfig defaultRequestConfig = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();

            //实例化CloseableHttpClient对象
            CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();*/

            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000).setConnectionRequestTimeout(1000)
                    .setSocketTimeout(5000).build();
            httpGet.setConfig(requestConfig);
            httpGet.setHeader("Accept", "*/*");
            httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");
            httpGet.setHeader("Connection", "keep-alive");
            httpGet.setHeader("User-Agent", USER_AGENT);
            httpGet.setHeader("Cache-Control", "no-cache");

            //控制时间
            HttpResponse response = httpclient.execute(httpGet);
            //System.out.println("得到的结果:" + response.getStatusLine());//得到请求结果
            //HttpEntity entity = response.getEntity();//得到请求回来的数据
            //ret = EntityUtils.toString(response.getEntity(), "utf-8");
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            System.out.println("body"+body);
            // 防封
            //root = JSONObject.parseObject(ret);
        } catch (Exception e) {
            e.printStackTrace();

            throw new Exception("请求url失败");
        }
        return root;
        /*HttpClient httpClient = getHttpClient();
        HttpGet get = new HttpGet(url);
        get.setHeader("User-Agent", USER_AGENT);
        get.setHeader("referer", "https://weibo.com/");
        get.setHeader("Cookie", cookie);
        JSONObject root = new JSONObject();
        try {
            HttpResponse response = httpClient.execute(get);
            //log.info(response.toString());
            String ret = EntityUtils.toString(response.getEntity(), "utf-8");
            // 防封
            root = JSONObject.parseObject(ret);
        } catch (Exception e) {
            //log.info("解析json出现异常"+page);
            e.printStackTrace();
            try {
                Thread.sleep(2000);
                //getImgURL(containerid, page,sinceId);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

        return root;*/
    }

    public static HttpClient getHttpClient() {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
        return httpClient;
    }



}
